package com.simple.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.simple.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.simple.common.annotation.Log;
import com.simple.common.core.controller.BaseController;
import com.simple.common.core.domain.AjaxResult;
import com.simple.common.enums.BusinessType;
import com.simple.project.domain.Comment;
import com.simple.project.service.ICommentService;
import com.simple.common.utils.poi.ExcelUtil;
import com.simple.common.core.page.TableDataInfo;

/**
 * 用户评论Controller
 * 
 * @author simple
 * @date 2020-12-01
 */
@RestController
@RequestMapping("/blog-project/comment" )
public class CommentController extends BaseController {

    @Autowired
    private ICommentService iCommentService;

    /**
     * 查询用户评论列表
     */
    @PreAuthorize("@ss.hasPermi('blog-project:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(Comment comment)
    {
        startPage();
        LambdaQueryWrapper<Comment> lqw = Wrappers.lambdaQuery(comment);
        if (StringUtils.isNotBlank(comment.getContent())){
            lqw.eq(Comment::getContent ,comment.getContent());
        }
        if (comment.getFirstCommentId() != null){
            lqw.eq(Comment::getFirstCommentId ,comment.getFirstCommentId());
        }
        if (comment.getUserId() != null){
            lqw.eq(Comment::getUserId ,comment.getUserId());
        }
        if (comment.getReplyCommentId() != null){
            lqw.eq(Comment::getReplyCommentId ,comment.getReplyCommentId());
        }
        if (comment.getReplyUserId() != null){
            lqw.eq(Comment::getReplyUserId ,comment.getReplyUserId());
        }
        if (comment.getBlogId() != null){
            lqw.eq(Comment::getBlogId ,comment.getBlogId());
        }
        if (comment.getIsDeleted() != null){
            lqw.eq(Comment::getIsDeleted ,comment.getIsDeleted());
        }
        List<Comment> list = iCommentService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出用户评论列表
     */
    @PreAuthorize("@ss.hasPermi('blog-project:comment:export')" )
    @Log(title = "用户评论" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Comment comment) {
        LambdaQueryWrapper<Comment> lqw = new LambdaQueryWrapper<Comment>(comment);
        List<Comment> list = iCommentService.list(lqw);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment. class);
        return util.exportExcel(list, "comment" );
    }

    /**
     * 获取用户评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog-project:comment:query')" )
    @GetMapping(value = "/{commentId}" )
    public AjaxResult getInfo(@PathVariable("commentId" ) Long commentId) {
        return AjaxResult.success(iCommentService.getById(commentId));
    }

    /**
     * 新增用户评论
     */
    @PreAuthorize("@ss.hasPermi('blog-project:comment:add')" )
    @Log(title = "用户评论" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Comment comment) {
        return toAjax(iCommentService.save(comment) ? 1 : 0);
    }

    /**
     * 修改用户评论
     */
    @PreAuthorize("@ss.hasPermi('blog-project:comment:edit')" )
    @Log(title = "用户评论" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Comment comment) {
        return toAjax(iCommentService.updateById(comment) ? 1 : 0);
    }

    /**
     * 删除用户评论
     */
    @PreAuthorize("@ss.hasPermi('blog-project:comment:remove')" )
    @Log(title = "用户评论" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{commentIds}" )
    public AjaxResult remove(@PathVariable Long[] commentIds) {
        return toAjax(iCommentService.removeByIds(Arrays.asList(commentIds)) ? 1 : 0);
    }
}
